package com.qf.push.service.impl;

import com.qf.push.dao.IdemDao;
import com.qf.push.dao.MessageDao;
import com.qf.push.model.Idem;
import com.qf.push.model.IdemExample;
import com.qf.push.model.Message;
import com.qf.push.service.PushService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class PushServiceImpl implements PushService {
    @Resource
    private MessageDao messageDao;
    @Resource
    private IdemDao idemDao;



    @Transactional
    @Override
    public int saveMessage(Message message, Long requestid) {
        //先不要动数据库，应该先判断requestId在数据库中存不存在
        IdemExample example=new IdemExample();
        IdemExample.Criteria criteria = example.createCriteria();
        criteria.andRequestidEqualTo(requestid);
        List<Idem> idems = idemDao.selectByExample(example);
        if (idems!=null&&idems.size()>0){
            return 1;
        }
        Idem idem=new Idem();
        idem.setRequestid(requestid);

        idemDao.insertSelective(idem);
        return messageDao.insertSelective(message);
    }

    @Override
    public boolean isIdem(Long requestId) {
        IdemExample example=new IdemExample();
        IdemExample.Criteria criteria = example.createCriteria();
        criteria.andRequestidEqualTo(requestId);
        List<Idem> idems = idemDao.selectByExample(example);
        if (idems!=null&&idems.size()>0){
            return false;
        }
        return true;
    }
}
